﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace Neurokinetic.NeuroSkyNet.Model
{
    [Serializable]
    /// <summary>
    /// Relevant data about current Brain Wave Activity.
    /// </summary>
    public class MindSetDataModel : INotifyPropertyChanged
    {
        /// <summary>
        /// Baseline Lowest Non-Zero relevant reading
        /// </summary>
        public float Lowest
        {
            get;
            private set;
        }
        /// <summary>
        /// Most resent relevant reading
        /// </summary>
        public float Current
        {
            get;
            private set;
        }
        /// <summary>
        /// Baseline hiest relevant reading
        /// </summary>
        public float Highest
        {
            get;
            private set;
        }
        /// <summary>
        /// Number of relivent metronomic ticks 
        /// </summary>
        public Int32 Ticks
        {
            get;
            private set;
        }
        /// <summary>
        /// MindSet Data Type
        /// </summary>
        public MindSetDataType DataType
        {
            get;
            private set;
        }
        /// <summary>
        /// Default Constructor
        /// </summary>
        /// <param name="dataType"></param>
        public MindSetDataModel(MindSetDataType dataType)
        {
            DataType = dataType;
        }
        /// <summary>
        /// Updates Current value and number of Ticks then derives Lowest and Highest values
        /// </summary>
        /// <param name="current">Relevant Reading</param>
        internal void Add(float current)
        {
            this.Ticks++;
            NotifyPropertyChanged("Ticks");
            if (this.Current != current)
            {
                this.Current = current;
                NotifyPropertyChanged("Current");
            }
            if (current < this.Lowest || this.Lowest == 0)
            {
                this.Lowest = current;
                NotifyPropertyChanged("Lowest");
            }

            if (current > this.Highest)
            {
                this.Highest = current;
                NotifyPropertyChanged("Highest");
            }
        }
        /// <summary>
        /// Calculates the current percent based on the Highest, Lowest, and Current values
        /// </summary>
        /// <returns></returns>
        public Double Percent()
        {
            float range = (this.Highest - this.Lowest);
            float offsetCurrent = (this.Current - this.Lowest);
            return (offsetCurrent == 0)?0:Math.Round(offsetCurrent / range * 100d);
        }
        #region | INotifyPropertyChanged Implementation |
        private void NotifyPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        public override string ToString()
        {
            return Current.ToString();
        }
    }
}
